#include "PlayListReader.h"

#include <QtXml>

#include <QMessageBox>
#include <QFile>

#include <iostream>

using namespace std;

PlayListReader :: PlayListReader(QObject *parent) :
        QObject(parent)
{
}


bool PlayListReader :: read(const QString &fileName, QStringList &dataOut)const
{
    return readXspf(fileName,dataOut);

}

bool PlayListReader :: readXspf(const QString &fileName, QStringList &dataOut)const
{
    QDomDocument doc(fileName.split(".").first());
    QFile xspfFile(fileName);

    if (!xspfFile.open(QIODevice::ReadOnly))
        return false;

    if (!doc.setContent(&xspfFile))
    {
        xspfFile.close();
        return false;
    }

    xspfFile.close();


    QDomElement version = doc.documentElement();

    if(version.attribute("version","NO_Version") != "1")
        return false; //bad version

    //si les version de playlist change il sera possible de choisir un lecteur en fonction

    QDomNode title = version.firstChild();   //title of the playlist (useless atm)

    QDomNode trackList = title.firstChild(); //Contain all track

    QDomNode track = trackList.firstChild(); //Contain all track

    dataOut << track.attributes().namedItem("location").toElement().text();

    //dataOut << track.firstChild().toElement().text();//"location","oo");

    /*while(!track.isNull())
    {
        if(track.tagName() == "track")
        {
            dataOut << track.firstChild().toElement().attribute("location","");

        }

        track = track.nextSibling().toElement();
    }*/








    return true;
}
